<template>
  <div>
    <div style="margin-bottom: 20px; margin-top: 20px; text-align: center">
      <el-input placeholder="请输入内容" v-model="selectValue" clearable style="width: 350px"></el-input>
      <el-button type="primary" icon="el-icon-search" round style="margin-left: 15px" @click="search">搜索</el-button>
      <el-button type="primary" icon="el-icon-refresh" round style="margin-left: 15px; margin-right: 15px" @click="reset">重置</el-button>
      <el-button type="primary" style="margin-left: 15px" @click="handleAdd">添加分区</el-button>
    </div>
    <el-table
        :data="tableData"
        tooltip-effect="dark"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="typeName" label="分区名称"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="deleteFileType(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          background
          :current-page="pageNum"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
<!--    添加分区弹窗-->
    <el-dialog
        title="添加分区"
        :visible.sync="dialogVisible"
        width="25%"
    >
      <el-form label-position="right" style="margin-left: 50px">
        <el-form-item label="分区名称:">
          <el-input v-model="form.typeName" autocomplete="off" style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveForm()">添 加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AdminFileType",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 5,
      total: 0,
      selectValue: '',
      dialogVisible: false,
      form: {},
    }
  },
  created() {
    this.loadTypeList();
  },
  methods: {
    loadTypeList() {
      this.request.get("/admin/file_type/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          selectValue: this.selectValue,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    //修改当前页触发
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      //修改后重新加载表单
      this.loadTypeList();
    },
    search() {
      this.pageNum = 1;
      this.loadTypeList();
    },
    reset() {
      this.selectValue = '';
      this.loadTypeList();
    },
    deleteFileType(id) {
      this.request.delete("/admin/file_type/" + id).then(res => {
        if (res.code == 200) {
          this.$message.success(res.msg);
          this.loadTypeList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    handleAdd() {
      this.form = {};
      this.dialogVisible = true;
    },
    saveForm() {
      this.request.post("/admin/file_type", this.form).then(res => {
        if (res.code == 200) {
          this.$message.success(res.msg);
          this.form = {};
          this.loadTypeList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
